﻿using AnesSystem.Model;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace AnesSystem.Controls.CommonTables.CQ.CQFY
{
    /// <summary>
    /// ShouShuShouFei.xaml 的交互逻辑
    /// </summary>
    public partial class JingMaiMaZuiCtrl : CustomCommonTableCtrl
    {
        private System.Windows.Threading.DispatcherTimer timer;
        private int i = 0;
        private PatientLisDataModel _patientLisData;
        public JingMaiMaZuiCtrl()
        {
            InitializeComponent();
            IniteRadioBation();
            InitalTimer();
            this.Loaded += thisCtrl_Loaded;
        }

        public PatientLisDataModel PatientLisData
        {
            get
            {
                return _patientLisData;
            }

            set
            {
                _patientLisData = value;
                RaisePropertyChanged("PatientLisData");
            }
        }
        private void thisCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            DataManager.Instance.IsNotNeedSaveData = false;
            FunctionListHeaderCtrlViewModel.Instance.DisplayButton(true, FunctionListButton.打印, FunctionListButton.刷新, FunctionListButton.检验数据);
        }

        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += Timer_Tick;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (i < 30)
            {
                PatientLisData = Util.ExtensionMethods.GetLisData();
                if (PatientLisData != null && PatientLisData.Result == true)
                {
                    FillLisData();
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                    i = 0;
                    timer.Stop();
                    return;
                }
                i++;
            }
            else
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                i = 0;
                timer.Stop();
                MessageBoxCommon.ShowDialog("获取数据完成，若未获取到检验数据，请稍等几分钟重试！！！", MessageBoxCommon.OK);
            }
        }

        public override void GetLisData()
        {
            string Zyh = DataManager.Instance.CurrentPatientLink.Patient.Zyh;
            if (Zyh == null || "".Equals(Zyh))
            {
                MessageBoxCommon.ShowDialog("请输入住院号重试！！！", MessageBoxCommon.OKCANCLE);
                return;
            }
            try
            {
                Task.Factory.StartNew(delegate
                {
                    PatientLisData = Util.ExtensionMethods.GetLisData();
                    if (PatientLisData != null)
                    {
                        //PatientLisDatas.Result = false;
                        //PatientLisDataService.Instance.AddUpdate(PatientLisDatas);
                        Dispatcher.Invoke(delegate
                        {
                            FillLisData();
                            MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                        });
                        return;
                    }
                    UdpModel udpModel = new UdpModel();
                    udpModel.PatientId = DataManager.Instance.CurrentPatientLink.Id.ToString();
                    udpModel.Zyh = Zyh;
                    udpModel.UpdateDate = DateTime.Now.ToString();
                    udpModel.AccountID = DataManager.Instance.CurrentPatientLink.Patient.Brxm;
                    UdpMangager.Instance.SendUdpServerMsg(ServiceSign.GETLISDATA, udpModel);
                    timer.Start();//执行查询方法的线程  
                });
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void FillLisData()
        {
            Dictionary<string, object> jianYanMap = Util.ExtensionMethods.GetJianYanMap(PatientLisData.JianYan);
            DataManager.Instance.FillCtrlValue(jianYanMap);
            DataManager.Instance.FillAndSaveXMLTableValue(jianYanMap);
        }


        private void rq_GotFocus(object sender, RoutedEventArgs e)
        {
            CusDatePickerWin cusDatePicker = new CusDatePickerWin();
            cusDatePicker.SelectedDateTime = DateTime.Now;
            cusDatePicker.ShowDialog();
            if (cusDatePicker.Result)
            {
                rq.Value = cusDatePicker.SelectedDateTime.ToString(DateFormat.DATE_FORMAT_STR_YMD_CN);//xxxx年xx月xx日
            }
        }

        private void rssj_GotFocus(object sender, RoutedEventArgs e)
        {
            if (VerifyOperRoom())
            {
                BindTextCtrl bdctrl = sender as BindTextCtrl;
                CusDatePickerWin cusDatePicker = new CusDatePickerWin();
                if (DataManager.Instance.CurrentPatientLink.Status.InRoomTime == new DateTime())
                {
                    cusDatePicker.SelectedDateTime = DateTime.Now;
                }
                else
                {
                    cusDatePicker.SelectedDateTime = DataManager.Instance.CurrentPatientLink.Status.InRoomTime;
                }
                cusDatePicker.ShowDialog();

                if (cusDatePicker.Result)
                {
                    bdctrl.Value = cusDatePicker.SelectedDateTime;//2005年11月5日
                    DataManager.Instance.TableHeaderCtrl._anesProcessCtrl.ViewModel.InitInRoom4IVAnes(cusDatePicker.SelectedDateTime);
                }
            }
        }
        private void sskssj_GotFocus(object sender, RoutedEventArgs e)
        {
            if (DataManager.Instance.CurrentPatientLink.Status.InRoomTime != new DateTime())
            {
                BindTextCtrl bdctrl = sender as BindTextCtrl;
                CusDatePickerWin cusDatePicker = new CusDatePickerWin();
                if (DataManager.Instance.CurrentPatientLink.Status.OperStartTime == new DateTime())
                {
                    cusDatePicker.SelectedDateTime = DateTime.Now;
                }
                else
                {
                    cusDatePicker.SelectedDateTime = DataManager.Instance.CurrentPatientLink.Status.OperStartTime;
                }
                cusDatePicker.ShowDialog();
                if (cusDatePicker.Result)
                {
                    bdctrl.Value = cusDatePicker.SelectedDateTime;//2005年11月5日
                    DateTime date = cusDatePicker.SelectedDateTime;
                    DataManager.Instance.CurrentPatientLink.Status.OperStartTime = date.AddSeconds(-date.Second).AddMilliseconds(-date.Millisecond);
                    //添加事件
                    //                    EventAndDrugWinViewModel.Instance.AddEvent(Constants.EVENT_OPERATION, date);
                    SaveOper2DB();

                }
            }
            else
            {
                MessageBoxCommon.ShowDialog("入室时间不能为空", MessageBoxCommon.OK);
                return;
            }
        }
        private void ssjssj_GotFocus(object sender, RoutedEventArgs e)
        {
            if (DataManager.Instance.CurrentPatientLink.Status.OperStartTime != new DateTime())
            {
                BindTextCtrl bdctrl = sender as BindTextCtrl;
                CusDatePickerWin cusDatePicker = new CusDatePickerWin();
                if (DataManager.Instance.CurrentPatientLink.Status.OperEndTime == new DateTime())
                {
                    cusDatePicker.SelectedDateTime = DateTime.Now;
                }
                else
                {
                    cusDatePicker.SelectedDateTime = DataManager.Instance.CurrentPatientLink.Status.OperEndTime;
                }
                cusDatePicker.ShowDialog();
                if (cusDatePicker.Result)
                {
                    bdctrl.Value = cusDatePicker.SelectedDateTime;//2005年11月5日
                    DateTime date = cusDatePicker.SelectedDateTime;
                    DataManager.Instance.CurrentPatientLink.Status.OperEndTime = date.AddSeconds(-date.Second).AddMilliseconds(-date.Millisecond);
                    //添加事件
                    //                    EventAndDrugWinViewModel.Instance.AddEvent(Constants.EVENT_OPERATION, date);
                    SaveOper2DB();
                }
            }
            else
            {
                MessageBoxCommon.ShowDialog("手术开始时间不能为空", MessageBoxCommon.OK);
                return;
            }
        }
        private void sscssj_GotFocus(object sender, RoutedEventArgs e)
        {
            if (DataManager.Instance.CurrentPatientLink.Status.OperEndTime != new DateTime())
            {
                if (DataManager.Instance.CurrentPatientLink.AnesthesiaRecord != null && string.IsNullOrEmpty(DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Mzff))
                {
                    MessageBoxCommon.ShowDialog("未选择麻醉方式", MessageBoxCommon.OK);
                    return;
                }
                BindTextCtrl bdctrl = sender as BindTextCtrl;
                CusDatePickerWin cusDatePicker = new CusDatePickerWin();
                if (DataManager.Instance.CurrentPatientLink.Status.OutRoomTime == new DateTime())
                {
                    cusDatePicker.SelectedDateTime = DateTime.Now;
                }
                else
                {
                    cusDatePicker.SelectedDateTime = DataManager.Instance.CurrentPatientLink.Status.OutRoomTime;
                }
                cusDatePicker.ShowDialog();
                if (cusDatePicker.Result)
                {
                    if (cusDatePicker.SelectedDateTime.CompareTo(DataManager.Instance.CurrentPatientLink.Status.InRoomTime) <= 0)
                    {
                        MessageBoxCommon.ShowDialog("出室时间应大于入室时间!", MessageBoxCommon.OK);
                        return;
                    }
                    else
                    {
                        bdctrl.Value = cusDatePicker.SelectedDateTime;//2005年11月5日
                        DataManager.Instance.TableHeaderCtrl._anesProcessCtrl.ViewModel.InitOutRoom4IVAnes(cusDatePicker.SelectedDateTime);
                    }
                }
            }
            else
            {
                MessageBoxCommon.ShowDialog("手术结束时间不能为空", MessageBoxCommon.OK);
                return;
            }
        }
        private bool VerifyOperRoom()
        {
            string loginRoom = DataManager.Instance.CurRoom.Name;
            if (string.IsNullOrEmpty(loginRoom))
            {
                return false;
            }
            if (!loginRoom.Equals(DataManager.Instance.CurrentPatientLink.Schedule.Pbssjh))
            {
                MessageBoxCommon.ShowDialog("此手术间只能进行" + loginRoom + "号手术间手术!", MessageBoxCommon.OK);
                return false;
            }
            List<PatientLinkModel> pList = PatientLinkService.Instance.InOperInRoom(loginRoom);
            if (pList == null) return true;
            PatientLinkModel op = pList.Find(delegate (PatientLinkModel p) { return p.Id == DataManager.Instance.CurrentPatientLink.Id; });//只返回第一个匹配
            if (op == null)
            {
                MessageBoxCommon.ShowDialog("当前手术间正在进行手术，无法进行新手术!", MessageBoxCommon.OK);
                return false;
            }
            else
            {
                return true;
            }
        }
        private void IniteRadioBation()
        {
            new RadioButtonHelper().ChangeRadioSatus(yis_qx, yis_dm, yis_hm);
            new RadioButtonHelper().ChangeRadioSatus(kunnqd_w, kunnqd_y);
            new RadioButtonHelper().ChangeRadioSatus(hux_zc, hux_yc);
            new RadioButtonHelper().ChangeRadioSatus(maib_yl, maib_xr);
            new RadioButtonHelper().ChangeRadioSatus(goums_w, goums_y);
            new RadioButtonHelper().ChangeRadioSatus(tesyys_w, tesyys_y);
            new RadioButtonHelper().ChangeRadioSatus(jinsqk_yjs, jinsqk_wjs);
            new RadioButtonHelper().ChangeRadioSatus(tesqk_w, tesqk_y);
            new RadioButtonHelper().ChangeRadioSatus(xuecg_wj, xuecg_zc, xuecg_yc);
            new RadioButtonHelper().ChangeRadioSatus(ningxgn_wj, ningxgn_zc, ningxgn_yc);
            new RadioButtonHelper().ChangeRadioSatus(dianjz_wj, dianjz_zc, dianjz_yc);
            new RadioButtonHelper().ChangeRadioSatus(xuet_wj, xuet_zc, xuet_yc);
            new RadioButtonHelper().ChangeRadioSatus(gangn_wj, gangn_zc, gangn_yc);
            new RadioButtonHelper().ChangeRadioSatus(shengd_wj, shengd_zc, shengd_yc);
            new RadioButtonHelper().ChangeRadioSatus(ecg_wj, ecg_zc, ecg_yc);
            new RadioButtonHelper().ChangeRadioSatus(xiongt_wj, xiongt_zc, xiongt_yc);
            new RadioButtonHelper().ChangeRadioSatus(asa_1, asa_2, asa_3, asa_4, asa_5, asa_6);
            new RadioButtonHelper().ChangeRadioSatus(shuhys_qx, shuhys_wqx);
            new RadioButtonHelper().ChangeRadioSatus(shuhsmtz_y, shuhsmtz_n);
            new RadioButtonHelper().ChangeRadioSatus(zcty_n, zcty_f);

        }

        private void SaveOper2DB()
        {
            PatientLinkService.Instance.AddUpdate(DataManager.Instance.CurrentPatientLink);
        }
    }
}
